package Servlet.manager;

import Dao.UserDao;
import Manage.Manager;
import Utils.mybatisUtils;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class managerseleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session=mybatisUtils.getSqlSession();
        UserDao dao=session.getMapper(UserDao.class);
        ServletContext application=request.getServletContext();
        String key=(String) application.getAttribute("key");
        String value=(String)application.getAttribute("value") ;
        String checked="checked";
        List<Manager> list=new ArrayList<>();
        PrintWriter pw=response.getWriter();
        //表格修饰样式
        pw.print("<link rel=\"stylesheet\" href=css/table.css type=\"text/css\">");
        pw.print("    <script type=\"text/javascript\" src=\"js/jquery-3.4.1.js\"></script>\n" +
                "    <script type=\"text/javascript\">\n" +
                "        $(function () {\n" +
                "            $.ajax({url:\"json\",dataType:\"json\",success:function (resp) {\n" +
                "                    $(\"#sell\").empty();\n" +
                "                    $(\"#sell\").append(\"<option>请选择删除用户</option>\")\n" +
                "                    $(resp).each(function (index,element) {\n" +
                "                        $(\"#sell\").append(\"<option>\"+element.username+\"</option>\");\n" +
                "                    })\n" +
                "                }})\n" +
                "            cliik=function(){\n" +
                "                if ($(\"#sell\").val()!==\"请选择删除用户\"){\n" +
                "                    confirm('您正在进行用户数据删除操作，请确认！！！')\n" +
                "                    document.getElementById(\"form\").submit();return true;\n" +
                "                }\n" +
                "                confirm('您正在进行用户数据删除操作，请确认！！！')\n" +
                "            }\n" +
                "        })\n" +
                "    </script>");
        if ("username".equals(key)&&value!=null)
            list=dao.selectall(value,null,null,null,null);
        else if ("phone".equals(key)&&value!=null)
            list=dao.selectall(null,value,null,null,null);
        else if ("address".equals(key)&&value!=null)
            list=dao.selectall(null,null,value,null,null);
        else if ("keyword".equals(key)&&value!=null)
            list=dao.selectall(null,null,null,value,null);
        else if ("street".equals(key)&&value!=null)
            list=dao.selectall(null,null,null,null,value);
        else
            list=dao.selectall(null,null,null,null,null);
        pw.print("        <table align=\"center\" border=\"1px\">\n" +
                "            <form action=/user/key>\n" +
                "            <tr>\n" +
                "                <th>搜索框</th>\n" +
                "                <th>\n" +
                "                    <select name=\"key\">\n" +
                "                        <option value=\"username\">用户名</option>\n" +
                "                        <option value=\"phone\">电话</option>\n" +
                "                        <option value=\"address\">地址</option>\n" +
                "                        <option value=\"keyword\">摄像头id</option>\n" +
                "                        <option value=\"street\">街道信息</option>" +
                "                    </select>\n" +
                "                </th>\n" +
                "                <th  colspan=\"2\"><input style=\"width: 210px\" type=\"text\" name=\"optioned\"><input type=\"submit\" value=\"搜索\"></th>\n" +
                "            </form>\n" +
                "            <form id=\"form\" action=/user/caozuo1>\n" +
                "                <th>\n" +
                "                   <input hidden type=\"text\" name=\"caozuo\" value=\"删用户\">\n" +
                "                    <select name=\"username\" id=\"sell\">\n" +
                "                        <option>请选择删除用户</option>\n" +
                "                    </select>\n" +
                "                    <input  type=\"button\" onclick=\"cliik()\" value=\"删用户\">\n" +
                "                </th>\n" +
                "                </form>"+
                "               <th align=\"right\" colspan=\"6\" style=\"height: 30px\"><form action=\"/user/key\"><input  name=\"show\" type=\"submit\" value=\"显示全部用户\"></form></th> "+
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>用户类型</th>\n" +
                "                <th>用户名</th>\n" +
                "                <th>密码</th>\n" +
                "                <th>性别</th>\n" +
                "                <th>电话</th>\n" +
                "                <th>地址</th>\n" +
                "                <th>摄像头数量</th>\n" +
                "                <th>摄像头id</th>\n" +
                "                <th>街道信息</th>\n" +
                "                <th colspan=\"2\"></th>" +
                "            </tr>\n");
        for (Manager x:list) {
            if ("男".equals(x.getSex())) {
                pw.print(
                        "            <form action=\"/user/caozuo1\" >\n" +
                                "                <tr>\n" +
                                "                    <td ><input style=\"width: 40px\" type=\"text\"  value="+x.getUser()+" name=\"user\" pattern=\"^(([s][u][p][e][r])|([p][u][b][l][i][c]))$\" ></td>\n" +
                                "                    <td><input  style=\"width: 70px\"  type=\"text\"  value="+x.getUsername()+" name=\"username\" pattern=\"^[A-Za-z0-9]{6,10}$\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getPassword()+" name=\"password\" pattern=\"^.{6,12}$\"></td>\n" +
                                "                    <td>\n" +
                                "                        男<input  type=\"radio\"  name=\"sex\" value=\"男\" " + checked + " >\n" +
                                "                        女<input name=\"sex\" type=\"radio\" value=\"女\"  >\n" +
                                "                    </td>\n" +
                                "                    <td><input style=\"width: 90px\" type=\"text\" value="+x.getPhone()+" name=\"phone\" pattern=\"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$\"></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getAddress()+" name=\"address\" ></td>\n" +
                                "                    <td><input style=\"width: 40px\" type=\"text\" value="+x.getNum()+" name=\"num\" pattern=\"^[0-9]*$\"></td>\n" +
                                "                    <td><input readonly style=\"width: 40px\" type=\"text\" value="+x.getKeyword()+" name=\"keyword\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getStreet()+" name=\"street\" ></td>\n" +
                                "                    <td hidden><input style=\"text\" name=\"idd\" value="+x.getIdd()+"></td>\n"+
                                "                    <td hidden><input style=\"text\" name=\"iddd\" value="+x.getIddd()+"></td>\n"+
                                "                    <td>\n" +
                                "                        <input class=\"mybt\" type=\"submit\" name=\"caozuo\" value=\"改\" >\n" +
                                "             </form>\n" +
                                "                    </td>\n" +
                                "                        <form action=\"/user/caozuo1\">\n" +
                                "                        <td hidden><input style=\"text\" name=\"keyword\" value="+x.getKeyword()+"></td>\n"+
                                "                    <td>\n" +
                                "                       <input class=\"mybt\" type=\"submit\"  name=\"caozuo\" onclick=\"return confirm('您正在进行摄像头数据删除操作，请确认！！！')\" value='删'>\n" +
                                "                    </td>" +
                                "                       </form>\n" +
                                "                </tr>");
            }
            else if ("女".equals(x.getSex())){
                pw.print(
                        "            <form action=\"/user/caozuo1\" >\n" +
                                "                <tr>\n" +
                                "                    <td ><input style=\"width: 40px\" type=\"text\"  value="+x.getUser()+" name=\"user\" pattern=\"^(([s][u][p][e][r])|([p][u][b][l][i][c]))$\" ></td>\n" +
                                "                    <td><input  style=\"width: 70px\"  type=\"text\"  value="+x.getUsername()+" name=\"username\" pattern=\"^[A-Za-z0-9]{6,10}$\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getPassword()+" name=\"password\" pattern=\"^.{6,12}$\"></td>\n" +
                                "                    <td>\n" +
                                "                        男<input  type=\"radio\"  name=\"sex\" value=\"男\"  >\n" +
                                "                        女<input name=\"sex\" type=\"radio\" value=\"女\" " + checked + " >\n" +
                                "                    </td>\n" +
                                "                    <td><input style=\"width: 90px\" type=\"text\" value="+x.getPhone()+" name=\"phone\" pattern=\"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$\"></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getAddress()+" name=\"address\" ></td>\n" +
                                "                    <td><input style=\"width: 40px\" type=\"text\" value="+x.getNum()+" name=\"num\" pattern=\"^[0-9]*$\"></td>\n" +
                                "                    <td><input readonly style=\"width: 40px\" type=\"text\" value="+x.getKeyword()+" name=\"keyword\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getStreet()+" name=\"street\" ></td>\n" +
                                "                    <td hidden><input style=\"text\" name=\"idd\" value="+x.getIdd()+"></td>\n"+
                                "                    <td hidden><input style=\"text\" name=\"iddd\" value="+x.getIddd()+"></td>\n"+
                                "                    <td>\n" +
                                "                        <input class=\"mybt\" type=\"submit\" name=\"caozuo\" value=\"改\" >\n" +
                                "             </form>\n" +
                                "                    </td>\n" +
                                "                        <form action=\"/user/caozuo1\">\n" +
                                "                        <td hidden><input style=\"text\" name=\"keyword\" value="+x.getKeyword()+"></td>\n"+
                                "                    <td>\n" +
                                "                       <input class=\"mybt\" type=\"submit\"  name=\"caozuo\" onclick=\"return confirm('您正在进行摄像头数据删除操作，请确认！！！')\" value='删'>\n" +
                                "                    </td>" +
                                "                       </form>\n" +
                                "                </tr>");
                }
            else{
                pw.print(
                        "            <form action=\"/user/caozuo1\" >\n" +
                                "                <tr>\n" +
                                "                    <td ><input style=\"width: 40px\" type=\"text\"  value="+x.getUser()+" name=\"user\" pattern=\"^(([s][u][p][e][r])|([p][u][b][l][i][c]))$\" ></td>\n" +
                                "                    <td><input  style=\"width: 70px\"  type=\"text\"  value="+x.getUsername()+" name=\"username\" pattern=\"^[A-Za-z0-9]{6,10}$\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getPassword()+" name=\"password\" pattern=\"^.{6,12}$\"></td>\n" +
                                "                    <td>\n" +
                                "                        男<input type=\"radio\"  name=\"sex\" value=\"男\"  >\n" +
                                "                        女<input name=\"sex\" type=\"radio\" value=\"女\"  >\n" +
                                "                    </td>\n" +
                                "                    <td><input style=\"width: 90px\" type=\"text\" value="+x.getPhone()+" name=\"phone\" pattern=\"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$\"></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getAddress()+" name=\"address\" ></td>\n" +
                                "                    <td><input style=\"width: 40px\" type=\"text\" value="+x.getNum()+" name=\"num\" pattern=\"^[0-9]*$\"></td>\n" +
                                "                    <td><input readonly style=\"width: 40px\" type=\"text\" value="+x.getKeyword()+" name=\"keyword\" ></td>\n" +
                                "                    <td><input style=\"width: 80px\" type=\"text\" value="+x.getStreet()+" name=\"street\" ></td>\n" +
                                "                    <td hidden><input style=\"text\" name=\"keyword\" value="+x.getKeyword()+"></td>\n"+
                                "                    <td hidden><input style=\"text\" name=\"idd\" value="+x.getIdd()+"></td>\n"+
                                "                    <td hidden><input style=\"text\" name=\"iddd\" value="+x.getIddd()+"></td>\n"+
                                "                    <td>\n" +
                                "                        <input class=\"mybt\" type=\"submit\" name=\"caozuo\" value=\"改\" >\n" +
                                "                    </td>\n" +
                                "             </form>\n" +
                                "                    </td>\n" +
                                "                        <form action=\"/user/caozuo1\">\n" +
                                "                        <td hidden><input style=\"text\" name=\"keyword\" value="+x.getKeyword()+"></td>\n"+
                                "                    <td>\n" +
                                "                       <input class=\"mybt\" type=\"submit\"  name=\"caozuo\" onclick=\"return confirm('您正在进行摄像头数据删除操作，请确认！！！')\" value='删'>\n" +
                                "                    </td>" +
                                "                       </form>\n" +
                                "                </tr>");
            }
            }
                pw.print("</table>");

        pw.flush();
        pw.close();
        session.close();
    }
}
